package com.xiaomuwu.android.ui.detail

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.xiaomuwu.android.R
import com.xiaomuwu.android.XiaoMuWuApplication
import com.xiaomuwu.android.logic.Repository
import com.xiaomuwu.android.logic.model.ForFollow
import com.xiaomuwu.android.logic.model.ForUserDetail
import com.xiaomuwu.android.ui.Displayer
import com.xiaomuwu.android.ui.ForSearch
import com.xiaomuwu.android.ui.LoginStatus
import kotlinx.android.synthetic.main.user_detail_fragment.*

class UserDetailFragment:Fragment() {

    private val viewModel by lazy{ViewModelProviders.of(this).get(
        UserDetailViewModel::class.java)}

    private var isFollowed=false    // 是否已关注

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.user_detail_fragment,container,false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel.userDetailLiveData.observe(viewLifecycleOwner, { result->
            userDetailName.text=Displayer.userName  // 显示用户名
            userDetailID.text="ID: ${Displayer.userID}" // 显示用户账号
            val userDetail=result.getOrNull()
            if(userDetail!=null){
                userResCount.text="上传: ${userDetail.resCount}"  // 显示上传数
                userFansCount.text="粉丝: ${userDetail.fansCount}"    // 显示粉丝数

                // 更新用户详情信息
                Displayer.fansCount=userDetail.fansCount

                if(userDetail.isFollowed==1){
                    follow.text="已关注"
                    isFollowed=true
                }else{
                    follow.text="关注"
                    isFollowed=false
                }
                Log.d("UserDetailFragment","UserDetail查询成功")
            }else{
                Log.d("UserDetailFragment","UserDetail查询失败")
            }
        })
        viewModel.getUserDetailLiveData(ForUserDetail(LoginStatus.user_ID,ForSearch.userID!!))

        // 关注操作：先更新UI上的文字，待退出用户详情页后再操作数据库
        // “关注” 按钮点击事件
        follow?.setOnClickListener{
            if(LoginStatus.user_ID==null) {
                Toast.makeText(XiaoMuWuApplication.context, "请先登录",
                    Toast.LENGTH_SHORT).show()
            }else {
                if(LoginStatus.user_ID!=ForSearch.userID){
                    if (isFollowed) {
                        follow.text = "关注"

                        // 更新UI粉丝数
                        Displayer.fansCount--
                        userFansCount.text = "粉丝: ${Displayer.fansCount}"

                        ForSearch.fansCountChange-- // 更新粉丝数变化
                        isFollowed=false
                    } else {
                        follow.text = "已关注"

                        // 更新UI粉丝数
                        Displayer.fansCount++
                        userFansCount.text = "粉丝: ${Displayer.fansCount}"

                        ForSearch.fansCountChange++ // 更新粉丝数变化
                        isFollowed=true
                    }
                }
            }
        }
    }

    override fun onPause() {
        super.onPause()

        // 执行用户详情页的操作
        if (ForSearch.fansCountChange == 1) {
            // 关注
            Repository.followUser(
                ForFollow(LoginStatus.user_ID!!,ForSearch.userID!!,"insert")
            )
        } else if (ForSearch.fansCountChange == -1) {
            // 取消关注
            Repository.followUser(
                ForFollow(LoginStatus.user_ID!!,ForSearch.userID!!,"delete")
            )
        }
        ForSearch.fansCountChange=0 // 重置关注数变化
    }

}